package edu.neelsys.business.userauth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import edu.neelsys.objectmodel.dbentites.User;
import edu.neelsys.objectmodel.userauth.LoggedUser;
import edu.neelsys.repository.user.UserRepository;

@Service("userAuthHandler")
public class UserAuthBean implements UserDetailsService {

	@Autowired
	UserRepository userRepository;

	public UserDetails loadUserByUsername(String userName)
			throws UsernameNotFoundException {
		User user = userRepository.findUserByUserName(userName);

		if (user == null) {
			throw new UsernameNotFoundException("User " + userName
					+ " not found.");
		}

		LoggedUser loggedUser = new LoggedUser(user);

		return loggedUser;
	}

}
